{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from lxml import etree\n",
    "html_str = \"\"\"\n",
    "<!DOCTYPE html>\n",
    "<html lang=\"en\">\n",
    "<head>\n",
    "    <meta charset=\"UTF-8\">\n",
    "    <title>网页名</title>\n",
    "</head>\n",
    "<body>\n",
    "    <div>\n",
    "        div-text\n",
    "        <span>span-text</span>\n",
    "        <a>a-text</a>\n",
    "        <p>p-text</p>\n",
    "    </div>\n",
    "    <table>\n",
    "        <tr>\n",
    "            <th>Heading</th>\n",
    "            <th>Another Heading</th>\n",
    "        </tr>\n",
    "        <tr>\n",
    "            <td>row 1, cell 1</td>\n",
    "            <td>row 1, cell 2</td>\n",
    "        </tr>\n",
    "        table-text-2\n",
    "    </table>\n",
    "</body>\n",
    "</html>\n",
    "\"\"\"\n",
    "\n",
    "html = etree.HTML(html_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['网页名']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 获取标签文本值\n",
    "html.xpath('/html/head/title/text()')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['网页名']\n",
      "[]\n",
      "[<Element body at 0x7f9c094d0a00>]\n",
      "[]\n",
      "----------------------------------------\n",
      "[<Element html at 0x7f9c0940dd20>]\n",
      "[]\n",
      "----------------------------------------\n",
      "[]\n",
      "[<Element body at 0x7f9c094d0a00>]\n",
      "[<Element head at 0x7f9c094cb370>]\n"
     ]
    }
   ],
   "source": [
    "print(html.xpath('head/title/text()'))\n",
    "print(html.xpath('html'))\n",
    "print(html.xpath('body'))\n",
    "print(html.xpath('title'))\n",
    "print('----' * 10)\n",
    "print(html.xpath('../html'))\n",
    "print(html.xpath('..'))\n",
    "print('----' * 10)\n",
    "print(html.xpath('./html'))\n",
    "print(html.xpath('./body'))\n",
    "print(html.xpath('./head'))\n",
    "\n",
    "## 结论：参数html的开始节点是<html>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[<Element html at 0x7f9c0940dd20>]\n",
      "[<Element body at 0x7f9c094d0e60>]\n",
      "[<Element head at 0x7f9c094cb370>]\n",
      "[<Element td at 0x7f9c094c1410>, <Element td at 0x7f9c094c1370>]\n",
      "[<Element td at 0x7f9c094c1410>, <Element td at 0x7f9c094c1370>]\n",
      "[<Element td at 0x7f9c094c1410>, <Element td at 0x7f9c094c1370>]\n"
     ]
    }
   ],
   "source": [
    "## 推荐的开始头部分\n",
    "## 绝对定位，从头开始\n",
    "print(html.xpath('/html'))\n",
    "print(html.xpath('body'))\n",
    "print(html.xpath('head'))\n",
    "print(html.xpath('/html/body/table/tr/td'))\n",
    "\n",
    "## 相对定位\n",
    "print(html.xpath('//table/tr/td'))\n",
    "## 推荐的写法，相对定位\n",
    "print(html.xpath('.//table/tr/td'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['span-text']\n",
      "['a-text']\n",
      "['p-text']\n"
     ]
    }
   ],
   "source": [
    "print(html.xpath('.//div/span/text()'))\n",
    "print(html.xpath('.//div/a/text()'))\n",
    "print(html.xpath('.//div/p/text()'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['\\n        div-text\\n        ', '\\n        ', '\\n        ', '\\n    ']\n"
     ]
    }
   ],
   "source": [
    "print(html.xpath('.//div/text()'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['\\n        div-text\\n        ', 'span-text', '\\n        ', 'a-text', '\\n        ', 'p-text', '\\n    ']\n"
     ]
    }
   ],
   "source": [
    "## div 下所有子级的文本\n",
    "print(html.xpath('.//div//text()'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
